import Clipboard from 'clipboard'
import config from '@/config'
import { Message } from 'iview'

const btns = {
  delete: (h, params, vm) => {
    return h('Poptip', {
      props: {
        confirm: true,
        transfer: true,
        title: '你确定要删除吗?'
      },
      on: {
        'on-ok': () => {
          if (config.preview) {
            Message.error('演示模式禁止删除')
          } else {
            vm.$emit('on-btn', 'delete', params)
          }
        }
      }
    }, [
      h('Button', {
        props: {
          type: 'text',
          ghost: true,
          size: 'small'
        }
      }, [
        h('Icon', {
          props: {
            type: 'md-trash',
            size: 18,
            color: '#000000'
          }
        })
      ])
    ])
  },
  edit: (h, params, vm) => {
    return h('Tooltip', {
      props: {
        content: '编辑',
        placement: 'top'
      }
    }, [
      h('Button', {
        props: {
          type: 'text',
          ghost: true,
          size: 'small'
        },
        on: {
          'click': () => {
            vm.$emit('on-btn', 'edit', params)
          }
        }
      }, [
        h('Icon', {
          props: {
            type: 'md-create',
            size: 18,
            color: '#000000'
          }
        })
      ])
    ])
  },
  lock: (h, params, vm) => {
    const type = params.row.status === 1 ? 'md-lock' : 'md-unlock'
    return h('Tooltip', {
      props: {
        content: '锁定',
        placement: 'top'
      }
    }, [
      h('Button', {
        props: {
          type: 'text',
          ghost: true,
          size: 'small'
        },
        on: {
          'click': () => {
            vm.$emit('on-btn', 'lock', params)
          }
        }
      }, [
        h('Icon', {
          props: {
            type: type,
            size: 18,
            color: '#000000'
          }
        })
      ])
    ])
  },
  copy: (h, params, vm) => {
    return h('span', {
      attrs: {
        'class': 'icon-copy',
        'data-clipboard-text': params.row.projectName
      },
      on: {
        'click': () => {
          let clipboard = new Clipboard('.icon-copy')
          clipboard.on('success', e => {
            Message.success('复制成功')
            clipboard.destroy()
          })
          clipboard.on('error', e => {
            Message.error('复制失败')
            clipboard.destroy()
          })
        }
      }
    }, params.row.projectName)
  },
  preview: (h, params, vm) => {
    return h('Tooltip', {
      props: {
        content: '预览',
        placement: 'top'
      }
    }, [
      h('Button', {
        props: {
          type: 'text',
          ghost: true,
          size: 'small'
        },
        on: {
          'click': () => {
            vm.$emit('on-btn', 'preview', params)
          }
        }
      }, [
        h('Icon', {
          props: {
            type: 'md-eye',
            size: 18,
            color: '#000000'
          }
        })
      ])
    ])
  },
  download: (h, params, vm) => {
    return h('Tooltip', {
      props: {
        content: '下载文件',
        placement: 'top'
      }
    }, [
      h('Button', {
        props: {
          type: 'text',
          ghost: true,
          size: 'small'
        },
        on: {
          'click': () => {
            vm.$emit('on-btn', 'download', params)
          }
        }
      }, [
        h('Icon', {
          props: {
            type: 'md-download',
            size: 18,
            color: '#000000'
          }
        })
      ])
    ])
  },
  up: (h, params, vm) => {
    return h('Tooltip', {
      props: {
        content: '上移',
        placement: 'top'
      }
    }, [
      h('Button', {
        props: {
          type: 'text',
          ghost: true,
          size: 'small'
        },
        on: {
          'click': () => {
            vm.$emit('on-btn', 'up', params)
          }
        }
      }, [
        h('Icon', {
          props: {
            type: 'ios-arrow-up',
            size: 18,
            color: '#000000'
          }
        })
      ])
    ])
  },
  down: (h, params, vm) => {
    return h('Tooltip', {
      props: {
        content: '下移',
        placement: 'top'
      }
    }, [
      h('Button', {
        props: {
          type: 'text',
          ghost: true,
          size: 'small'
        },
        on: {
          'click': () => {
            vm.$emit('on-btn', 'down', params)
          }
        }
      }, [
        h('Icon', {
          props: {
            type: 'ios-arrow-down',
            size: 18,
            color: '#000000'
          }
        })
      ])
    ])
  }
}
export default btns
